// /src/pages/hot/hot.vue
<script setup lang="ts">
import { getHotRecommendAPI } from '@/service/home'
import type { HotParams, HotResult } from '@/types/home'
import { onLoad } from '@dcloudio/uni-app'
import { ref } from 'vue'

// 热门推荐页 标题和url
const hotMap = [
  { type: '1', title: '特惠推荐', url: '/hot/preference' },
  { type: '2', title: '爆款推荐', url: '/hot/inVogue' },
  { type: '3', title: '一站买全', url: '/hot/oneStop' },
  { type: '4', title: '新鲜好物', url: '/hot/new' },
]
// 获取路径上的变量 uniapp中
const query = defineProps<{
  type: string
}>()
// 应该获取哪个内容
const hot = hotMap.find((item) => item.type === query.type)
uni.setNavigationBarTitle({ title: hot!.title })
// 接口传参
const hotParam: HotParams = {
  page: 1,
  pageSize: 10,
  subType: hot?.type,
}
// 热门推荐的对象
const hotResult = ref<HotResult>({} as HotResult)
// 获得热门推荐的方法
const getHotRecommend = async () => {
  const res = await getHotRecommendAPI(hot!.url, hotParam)
  hotResult.value = res.result
}
// 当前激活的
const activeIndex = ref(0)
const changeTag = (index: number) => {
  activeIndex.value = index
}
// 是否正在加载
const isLoading = ref(true)
// 触底加载方法
const onScrollToLower = () => {}

onLoad(() => {
  getHotRecommend()
})
</script>

<template>
  <view class="viewport">
    <!-- 推荐封面图 -->
    <view class="cover">
      <image :src="hotResult.bannerPicture"></image>
    </view>
    <!-- 推荐选项 -->
    <view class="tabs">
      <text
        class="text"
        :class="{ active: index === activeIndex }"
        v-for="(item, index) in hotResult.subTypes"
        @tap="changeTag(index)"
        >{{ item.title }}</text
      >
    </view>
    <!-- 推荐列表 -->
    <scroll-view
      scroll-y
      class="scroll-view"
      v-for="(item, index) in hotResult.subTypes"
      :key="item.id"
      v-show="activeIndex === index"
      @scrolltolower="onScrollToLower"
    >
      <view class="goods">
        <navigator
          hover-class="none"
          class="navigator"
          v-for="good in 10"
          :key="good"
          :url="`/pages/goods/goods?id=`"
          @tap=""
        >
          <image
            class="thumb"
            src="https://yanxuan-item.nosdn.127.net/5e7864647286c7447eeee7f0025f8c11.png"
          ></image>
          <view class="name ellipsis">不含酒精，使用安心爽肤清洁湿巾</view>
          <view class="price">
            <text class="symbol">¥</text>
            <text class="number">29.90</text>
          </view>
        </navigator>
      </view>
      <view class="loading-text">{{ isLoading ? '正在加载' : '已经到底了~~' }} </view>
    </scroll-view>
  </view>
</template>

<style lang="scss">
.viewport {
  display: flex;
  flex-direction: column;
  height: 100%;
  padding: 180rpx 0 0;
  position: relative;
}
.cover {
  width: 750rpx;
  height: 225rpx;
  border-radius: 0 0 40rpx 40rpx;
  overflow: hidden;
  position: absolute;
  left: 0;
  top: 0;
  image {
    height: 100px;
  }
}
.scroll-view {
  // flex: 1;
  height: 100%;
}
.tabs {
  display: flex;
  justify-content: space-evenly;
  height: 100rpx;
  line-height: 90rpx;
  margin: 0 20rpx;
  font-size: 28rpx;
  border-radius: 10rpx;
  box-shadow: 0 4rpx 5rpx rgba(200, 200, 200, 0.3);
  color: #333;
  background-color: #fff;
  position: relative;
  z-index: 9;
  .text {
    margin: 0 20rpx;
    position: relative;
  }
  .active {
    &::after {
      content: '';
      width: 40rpx;
      height: 4rpx;
      transform: translate(-50%);
      background-color: #27ba9b;
      position: absolute;
      left: 50%;
      bottom: 24rpx;
    }
  }
}
.goods {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  padding: 0 20rpx 20rpx;
  .navigator {
    width: 316rpx;
    padding: 20rpx;
    margin-top: 20rpx;
    border-radius: 10rpx;
    background-color: #fff;
  }
  .thumb {
    width: 305rpx;
    height: 305rpx;
  }
  .name {
    height: 88rpx;
    font-size: 26rpx;
  }
  .price {
    line-height: 1;
    color: #cf4444;
    font-size: 30rpx;
  }
  .symbol {
    font-size: 70%;
  }
  .decimal {
    font-size: 70%;
  }
}

.loading-text {
  text-align: center;
  font-size: 28rpx;
  color: #666;
  padding: 20rpx 0 50rpx;
}
</style>
